const db = wx.cloud.database()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    name: '',
    address: '',
    labels: [],
    imgList:['cloud://zjn-0gzb74lr2a7692b1.7a6a-zjn-0gzb74lr2a7692b1-1317297115/image/add.png'],
    chooseFlag: false,
    previewFlag: false,
    editFlag: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (this.data.previewFlag || this.data.chooseFlag) {
      this.setData({ 
        previewFlag: false,
        chooseFlag: false
      })
      return
    }

    // 获取当前小程序的页面栈 
    let pages = getCurrentPages(); 
    // 数组中索引最大的页面--当前页面  
    let currentPage = pages[pages.length-1]
    var options = currentPage.options
    if (typeof(options.id) !== 'undefined') {
      var that = this
      db.collection('cinema').where({
        _id: options.id
      }).get().then(res => {
        that.setData({
          id: res.data[0]._id,
          name: res.data[0].funTitle,
          address: res.data[0].place,
          label: res.data[0].label,
          editFlag: true
        })
      })
    } else {
      this.setData({
        name: '',
        address: '',
        label: ['', '', '', '', ''],
        editFlag: false
      })
    }
  },

  async selectImg(e) {
    var idx = e.currentTarget.dataset.index
    var that = this
    if (idx !== this.data.imgList.length - 1) {
      var urls = []
      urls.push(that.data.imgList[idx])
      that.setData({previewFlag: true})
      await wx.previewImage({
        current: urls[0],
        urls: urls
      })
      
      return
    }

    var cnt = 2 - this.data.imgList.length
    that.setData({chooseFlag: true})
    await wx.chooseImage({
      count: cnt,
      sourceType:['album', 'camera'],
      success(res) {
        var imgList = that.data.imgList
        var imgUrl = res.tempFilePaths[0]
        imgList.splice(imgList.length - 1, 0, imgUrl)
        that.setData({imgList: imgList})
      },
      fail(res) {
        return
      }
    })
  },

  async deleteImg(e) {
    var idx = e.currentTarget.dataset.index
    if (this.data.imgList.length < 6 && idx === this.data.imgList.length - 1) {
      return
    }

    var flag = false
    await wx.showModal({
      title: '提示',
      content: '确认要删除照片吗'
    }).then(res => {
      if (res.cancel) {
        flag = true
      }
    })

    if (flag) {
      return
    }

    var imgList = this.data.imgList
    imgList.splice(idx, 1)
    this.setData({ imgList: imgList })
  },

  async onSubmit(e) {
    var that = this
    var name = e.detail.value.name
    var address = e.detail.value.address
    var label = []
    if (e.detail.value.label1.length > 0) {
      label.push(e.detail.value.label1)
    }
    if (e.detail.value.label2.length > 0) {
      label.push(e.detail.value.label2)
    }
    if (e.detail.value.label3.length > 0) {
      label.push(e.detail.value.label3)
    }
    if (e.detail.value.label4.length > 0) {
      label.push(e.detail.value.label4)
    }
    if (e.detail.value.label5.length > 0) {
      label.push(e.detail.value.label5)
    }
    label.sort(function(a, b){ return a.length - b.length })
    console.log(label)
    for (let i = label.length; i < 5; ++i) {
      label.push('')
    }

    if (name.length === 0 || address.length === 0) {
      wx.showToast({
        title: '请填写内容',
        icon: 'error',
        duration: 1000
      })

      return
    }

    if (this.data.editFlag) {
      await db.collection('cinema').doc(that.data.id).update({
        data: {
          funTitle: name,
          label: label,
          place: address
        }
      }).then(res => {
        wx.showToast({
          title: '编辑成功',
          icon: 'success',
          duration: 1000
        })
      })

      return
    }

    await db.collection('cinema').add({
      data: {
        funTitle: name,
        label: label,
        place: address
      }
    }).then(res => {
      wx.showToast({
        title: '保存成功',
        icon: 'success',
        duration: 1000
      })
    })
  }
})